package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.BookCatalogEntity;
import com.jifeng.br.pjj.bookreader.entity.RecordEntity;
import com.jifeng.br.pjj.bookreader.mapper.BookCatalogMapper;
import com.jifeng.br.pjj.bookreader.service.BookCatalogService;
import com.jifeng.br.pjj.bookreader.service.RecordService;
import com.jifeng.br.pjj.bookreader.util.R;
import com.jifeng.br.pjj.bookreader.util.exception.RTException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 图书章节表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@Service
@Transactional
public class BookCatalogServiceImpl extends ServiceImpl<BookCatalogMapper, BookCatalogEntity> implements BookCatalogService {
    @Autowired
    private RecordService recordService;

    @Override
    public IPage<BookCatalogEntity> getPage(int page, int limit, BookCatalogEntity bookCatalog) {
        String startTime = "";
        String endTime = "";
        boolean flag = false;
        if (bookCatalog.getApplyTimeRange() != null && bookCatalog.getApplyTimeRange().length == 2) {
            flag = true;
            startTime = bookCatalog.getApplyTimeRange()[0];
            endTime = bookCatalog.getApplyTimeRange()[1];
        }

        return this.page(new Page<>(page, limit), new QueryWrapper<BookCatalogEntity>()
                .eq("book_id", bookCatalog.getBookId())
                .like(StringUtils.isNotEmpty(bookCatalog.getCatalogName()), "catalog_name", bookCatalog.getCatalogName())
                .eq(bookCatalog.getCatalogStatus() != null, "catalog_status", bookCatalog.getCatalogStatus())
                .eq(bookCatalog.getStatus() != null, "status", bookCatalog.getStatus())
                .between(flag, "apply_time", startTime, endTime)
                .orderByDesc("id")
        );
    }

    @Override
    public List<BookCatalogEntity> getList(int bookId) {
        return this.list(new QueryWrapper<BookCatalogEntity>()
                .eq("book_id", bookId)
                .eq("catalog_status", 1)
                .eq("status", 1)
                .lt("apply_time", new Date())
                .orderByAsc("id")
        );
    }

    @Override
    public R getCatalogByUser(RecordEntity record) {
        BookCatalogEntity catalog = null;
        if (record.getCatalogId() != null) {
            catalog = this.getById(record.getCatalogId());
        } else {
            RecordEntity recordEntity = recordService.getOne(new QueryWrapper<RecordEntity>().eq("user_id", record.getUserId()).eq("book_id", record.getBookId()));
            if (recordEntity == null) {
                List<BookCatalogEntity> catalogList = this.list(new QueryWrapper<BookCatalogEntity>().eq("book_id", record.getBookId()).orderByAsc("id"));
                if (catalogList.size() > 0) {
                    catalog = catalogList.get(0);
                    record.setCatalogId(catalog.getId());
                } else {
                    throw new RTException("暂无章节可读");
                }
            } else {
                catalog = this.getById(recordEntity.getCatalogId());
                record.setCatalogId(catalog.getId());
            }
        }
        record.setReadTime(new Date());
        recordService.saveRecord(record);
        Map<String, Object> map = new HashMap<>();
        map.put("catalog", catalog);

        /*获取图书的所有章节*/
        List<BookCatalogEntity> list = this.getList(record.getBookId());
        for (int i = 0; i < list.size(); i++) {
            if (catalog.getId() == list.get(i).getId()) {
                if (i > 0) {
                    map.put("prevId", list.get(i - 1).getId());
                }
                if (i < list.size() - 1) {
                    map.put("nextId", list.get(i + 1).getId());
                }
            }
        }
        return R.data(map);
    }
}
